<template>
  <div class="dialogBox">
    <!-- 用户功能授权弹框 -->
    <el-dialog title="用户角色授权" :visible.sync="dialogVisible" :before-close="handleClose" :close-on-click-modal="false" :close-on-press-escape="false" width="76%">
      <el-container style="padding:10px; height: 320px;">
        <!-- 侧边栏表格 -->
        <el-aside style="float: left;width: 46%;">
          <el-card class="box-card el-card-flex">
            <div slot="header" class="clearfix">
              <el-form label-width="120px" :inline="true" class="el-flex-none" style="height: 0;">
                <el-form-item prop="roleCode" label="角色编码/名称:">
                  <el-col :span="25">
                    <el-input placeholder="请填写角色编码/名称"></el-input>
                  </el-col>
                </el-form-item>
                <el-button class="el-button-pad" type="primary">
                  查询
                  <i class="el-icon-search"></i>
                </el-button>
              </el-form>
            </div>
            <!-- aside的表格 -->
            <div class="text item">
              <div class="el-flex el-box-column">
                <el-table
                  :data="tableData"
                  :default-sort="{ prop: 'date', order: 'descending' }"
                  border
                  stripe
                  style="width: 100%"
                  :pageSize="pageSize"
                  @selection-change="handleRoleChange"
                  height="100%"
                  ref="tableRule"
                >
                <el-table-column type="selection" align="center"></el-table-column>
                <el-table-column :show-overflow-tooltip="true" fixed prop="roleCode" label="角色编码"></el-table-column>
                <el-table-column :show-overflow-tooltip="true" prop="roleName" label="角色名称"></el-table-column>
                <el-table-column :show-overflow-tooltip="true" prop="roleDesc" label="角色说明"></el-table-column>
              </el-table>
            </div>
            <div class="foot">
              <!-- 分页 -->
              <el-pagination
                background
                :current-page="nowPageNum"
                @current-change="currentChange"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total="roleData.length"
              ></el-pagination>
              <!-- el-user-info -->
              <div class="btns">
                <el-button type="text" class="chose" @click="choseUser" v-show="ruleShow">已选角色 <em> {{this.num}} </em>条</el-button>
              </div>
            </div>
            </div>
          </el-card>
        </el-aside>
        <!-- 内容里的表格展示 -->
        <el-main style="padding: 0px 0px 0px 10px;">
          <el-card class="box-card el-card-flex">
            <div slot="header" class="clearfix">
              <el-form label-width="60px" :inline="true" class="el-flex-none" style="height: 0;">
                <el-form-item label="机构:">
                  <el-select v-model="formInline.region" placeholder="选择机构" style="width: 140px;">
                    <el-option label="机构1" value="shanghai"></el-option>
                    <el-option label="机构2" value="beijing"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="userName" label="用户名:">
                  <el-col style="width: 140px;">
                    <el-input placeholder="请填用户名称"></el-input>
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary">
                    查询
                    <i class="el-icon-search"></i>
                  </el-button>
                </el-form-item>
              </el-form>
            </div>
            <div class="text item">
              <!-- tabel表格 -->
              <div class="el-flex el-box-column">
                <el-table
                  :data="tableData"
                  :default-sort="{ prop: 'date', order: 'descending' }"
                  border
                  stripe
                  style="width: 100%"
                  :pageSize="pageSize"
                  @selection-change="handleUsernameChange"
                  height="100%"
                  ref="tableUser"
                >
                  <el-table-column type="selection" align="center"></el-table-column>
                  <el-table-column :show-overflow-tooltip="true" fixed prop="userName" label="用户名"></el-table-column>
                  <el-table-column :show-overflow-tooltip="true" prop="realName" label="员工姓名"></el-table-column>
                </el-table>
              </div>
              <div class="foot">
                <!-- 分页 -->
                <el-pagination
                  background
                  :current-page="nowPageNum"
                  @current-change="currentChange"
                  :page-size="pageSize"
                  layout="total, prev, pager, next, jumper"
                  :total="roleData.length"
                ></el-pagination>
                <!-- el-user-info -->
                <div class="btns">
                  <el-button type="text" class="chose" @click="choseUser" v-show="usernameShow">已选用户 <em> {{this.num}} </em>条</el-button>
                </div>
              </div>
            </div>
          </el-card>
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="authorization">授权 <i class="el-icon-printer"></i></el-button>
        <el-button type="primary" plain @click="revocation">撤销 <i class="el-icon-refresh"></i></el-button>
        <el-button type="danger" plain @click="handleCheck(false)">关闭 <i class="el-icon-close"></i></el-button>
      </span>
    </el-dialog>
    <!-- 批量管理弹框 -->
    <el-dialog
      title="批量管理"
      :visible.sync="dialogTableVisible"
      width="50%"
      :before-close="handleTabelClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      >
      <div style="height: 240px">
        <el-input v-model="searchValue" placeholder="请搜索..." suffix-icon="el-icon-search" style="margin-bottom: 12px;"></el-input>
        <el-table
          stripe
          :data="tableData"
          height="212px"
          :show-header="false"
          ref="singleTable"
          @selection-change="handlDialogChange"
        >
          <el-table-column type="selection" align="center"></el-table-column>
          <el-table-column fixed prop="userName" label="用户名" align="left"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="removeDatas(scope.$index, tableData)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer dialogFooter">
        <!-- <el-checkbox label="全选" name="type"></el-checkbox> -->
        <el-checkbox v-model="checkeAll" @change="handleCheckAllChange(tableData)">全选</el-checkbox>
        <el-button type="primary" @click="handleClearDatas">清空</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { Tree, Dialog, Table, TableColumn, Pagination, MessageBox, Badge, Message } from 'element-ui'
export default {
  props: ['dialogVisible'],
  components: {
    'el-tree': Tree, // 树形菜单
    'el-dialog': Dialog, // 对话框
    'el-table': Table, // 表格
    'el-table-column': TableColumn, // 表格列
    'el-pagination': Pagination, // 分页
    'el-badge': Badge
  },
  data () {
    return {
      activeName: '1',
      formInline: {
        roleName: ''
      },
      roleData: [
        {
          id: 1,
          roleCode: 'WFW',
          roleName: '员工',
          roleDesc: '2019信息',
          userName: '用户名',
          realName: '员工名1'
        },
        {
          id: 2,
          roleCode: 'CST',
          roleName: '员工2',
          roleDesc: '2019信息',
          userName: '用户名',
          realName: '员工名2'
        }
      ],
      nowPageNum: 0,
      tableData: [],
      pageSize: 20,
      ruleShow: false, // 是否显示已选中角色
      usernameShow: false, // 是否显示已选中用户
      roleDatas: [], // 选中角色存放的数组
      usernameDatas: [], // 选中用户名存放的数组
      dialogeDatas: [], // 选中弹框里的tabel复选框存放的数组
      searchValue: '', // 搜索默认值
      dialogTableVisible: false, // 已选弹框是否显示
      allSelect: false, // 列表全选与否
      clearAll: [], // 清空所有选项
      num: 0, // 选中条数量
      checkeAll: false // 批量管理弹窗中的全选复选框
    }
  },
  mounted () {
    this.currentChange(1)
  },
  methods: {
    // 点击角色复选框
    handleRoleChange (val) {
      this.roleDatas = val
      if (this.roleDatas && this.roleDatas.length > 0) {
        this.ruleShow = true
        this.num = this.roleDatas.length
      } else {
        this.ruleShow = false
        this.num = 0
      }
    },
    // 点击用户名复选框
    handleUsernameChange (val) {
      this.usernameDatas = val
      if (this.usernameDatas && this.usernameDatas.length > 0) {
        this.usernameShow = true
        this.num = this.usernameDatas.length
      } else {
        this.usernameShow = false
        this.num = 0
      }
    },
    // 弹框表格复选框
    handlDialogChange (val) {
      this.dialogeDatas = val
      if (this.dialogeDatas.length && this.dialogeDatas.length === this.tableData.length) this.checkeAll = true
      else this.checkeAll = false
    },
    // 修改页码
    currentChange (newPageSize) {
      this.nowPageNum = newPageSize
      console.log(newPageSize)
      var start = (newPageSize - 1) * this.pageSize
      var end = start + this.pageSize
      this.tableData = this.roleData.slice(start, end)
    },
    // 关闭弹框
    handleClose () {
      MessageBox.confirm('确认要关闭弹框吗？')
        .then(() => {
          this.$emit('dialogHide', false)
        })
        .catch(() => {})
    },
    // 编辑弹框点击按钮
    handleCheck (result) {
      if (!result) {
        return this.handleClose()
      }
      // this.$emit('dialogHide', this.nowCheckObj)
    },
    // 已选弹框
    choseUser () {
      this.dialogTableVisible = true
    },
    // 取消弹框事件
    handleTabelClose () {
      this.dialogTableVisible = false
    },
    // 移除事件
    removeDatas (index, rows) {
      MessageBox.confirm('您是否确定移除此行内容', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rows.splice(index, 1)
        Message({
          type: 'success',
          message: '移除成功!'
        })
      }).catch(() => {
        Message({
          type: 'info',
          message: '已取消移除'
        })
      })
    },
    // 全选事件
    handleCheckAllChange (rows) {
      // console.log(rows)
      if (rows) {
        rows.forEach(row => {
          this.clearAll = row
          console.log(this.clearAll)
          this.$refs.singleTable.toggleRowSelection(this.clearAll, !this.allSelect)
        })
        this.allSelect = !this.allSelect
      }
    },
    // 清空事件
    handleClearDatas () {
      MessageBox.confirm('您是否确定清空所有数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var idArr = [] // 需要删除的id的数组
        this.dialogeDatas.forEach(v => {
          idArr.push(v.id)
        })
        var length = this.tableData.length // 总长度
        // 遍历总长度，边遍历边改变数组长度，方便计算索引值
        for (var i = length - 1; i >= 0; i--) {
          if (!idArr.length) break // 如果删除的数据空了 就直接break出去
          // 遍历需要删除的id数组，删除对应数据
          for (var k = 0; k < idArr.length; k++) {
            if (this.tableData[i].id === idArr[k]) {
              this.tableData.splice(i, 1)
              idArr.splice(k, 1)
              break
            }
          }
        }
        Message({
          type: 'success',
          message: '清空成功!'
        })
      }).catch(() => {
        Message({
          type: 'info',
          message: '取消清空'
        })
      })
    },
    // 授权事件
    authorization () {
      MessageBox.confirm('您授权成功', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      })
        .then(() => {})
        .catch(() => {})
    },
    // 撤销事件
    revocation () {
      this.$refs.tableRule.clearSelection()
      this.$refs.tableUser.clearSelection()
    }
  }
}
</script>
<style lang="scss">
.el-badge__content.is-fixed {
  position: absolute;
  top: 8px;
  right: 18px;
}
.foot{
  display: flex;
  justify-content: space-between;
  align-items: center;
  .chose{
    margin-top: 10px;
    color: #606266;
    font-size: 14px;
    em{
      font-style: normal;
      color: #409eff;
    }
  }
}
.dialogFooter{
  display: flex;
  justify-content: space-between;
  padding: 0 16px;
}
</style>
